#include "LRESItemWidget.h"
#include "../std.h"

LRESItemWidget::LRESItemWidget(int x, int y, int width, int height, Widget* parent)
	   : Widget(x, y, width, height, parent),
	     mListening(false)
{
	mCompanyName = new Label(0, 0, width, height, this);
	mCompanyName->setDrawBackground(false);
	mCompanyName->setMultiLine(true);
	mCompanyName->setAutoSizeX(false);
	mCompanyName->setAutoSizeY(true);
	mCompanyName->setPaddingLeft(3);
	mCompanyName->setPaddingRight(3);
	mCompanyName->setPaddingBottom(3);
	mDrawDate = new Label(0, 0, width, height, this);
	mDrawDate->setCaption(" ");
	mDrawDate->setDrawBackground(false);
	mDrawDate->setMultiLine(true);
	mDrawDate->setAutoSizeX(false);
	mDrawDate->setAutoSizeY(true);
	mDrawDate->setPaddingLeft(3);
	mDrawDate->setPaddingRight(3);
	mDrawDate->setPaddingBottom(3);
}

LRESItemWidget::~LRESItemWidget()
{
	if(mListening)
		Environment::getEnvironment().removePointerListener(this);
}

void LRESItemWidget::setCompanyName(String& text)
{
	mCompanyName->setCaption(text);
	mCompanyName->update();
	this->setHeight(mCompanyName->getHeight());
}

void LRESItemWidget::setCompanyFont(Font* font)
{
	mCompanyName->setFont(font);
	mCompanyName->update();
	this->setHeight(mCompanyName->getHeight());
}

void LRESItemWidget::setDrawDate(String& text)
{
	mDrawDate->setCaption(text);
	mDrawDate->update();
}

void LRESItemWidget::setDrawDateFont(Font* font)
{
	mDrawDate->setFont(font);
	mDrawDate->update();
}

void LRESItemWidget::setId(String& text){
	mId = text;
}
void LRESItemWidget::setSmsCnt(String& text){
	mSmscnt = text;
}
void LRESItemWidget::setShortCode(String& text){
	mShortcode = text;
}
void LRESItemWidget::setResult(String& text){
	mResult = text;
}
void LRESItemWidget::setIsToday(String& text){
	mIstoday = text;
}
void LRESItemWidget::setIsOod(String& text){
	mIsood = text;
}

const String& LRESItemWidget::getCompanyName(){
	return mCompanyName->getCaption();
}
const String& LRESItemWidget::getDrawDate(){
	return mDrawDate->getCaption();
}
String& LRESItemWidget::getId(){
	return mId;
}
String& LRESItemWidget::getSmsCnt(){
	return mSmscnt;
}
String& LRESItemWidget::getShortCode(){
	return mShortcode;
}
String& LRESItemWidget::getResult(){
	return mResult;
}
String& LRESItemWidget::getIsToday(){
	return mIstoday;
}
String& LRESItemWidget::getIsOod(){
	return mIsood;
}

void LRESItemWidget::setSelected(bool selected)
{
	if(selected)
	{
		this->setHeight(mCompanyName->getHeight() + mDrawDate->getHeight());
		mDrawDate->setPosition(0, mCompanyName->getHeight());
	}
	else
		this->setHeight(mCompanyName->getHeight());

	Widget::setSelected(selected);

	if(selected)
	{
		if(!mListening)
		{
			Environment::getEnvironment().addPointerListener(this);
			mListening = true;
		}
	}
	else
	{
		if(mListening)
		{
			Environment::getEnvironment().removePointerListener(this);
			mListening = false;
		}
	}
	this->getParent()->update();
}

void LRESItemWidget::pointerPressEvent(MAPoint2d p)
{}

void LRESItemWidget::pointerMoveEvent(MAPoint2d p)
{}

void LRESItemWidget::pointerReleaseEvent(MAPoint2d p)
{
	/*mNextImage->update();
	KineticListBox* klb = (KineticListBox*)this->getParent();
	Point testpos = mNextImage->getPosition();

	//If you reuse this component, and your widget hierarchy is different, then you
	//will need to recalculate this.
	int adjustedY = testpos.y + this->getPosition().y + this->getParent()->getPosition().y + (klb->getYOffset() >> 16);

	//Inform WidgetListeners if the next item is pressed
	if(mNextImage != NULL)
		if(p.x >= testpos.x && p.x <= testpos.x + mNextImage->getWidth()
		&& p.y >= adjustedY && p.y <= adjustedY + mNextImage->getHeight())
			trigger();*/
}

//Make the appropriate widgets visible when selected.
void LRESItemWidget::drawWidget()
{
	mDrawDate->setEnabled(this->isSelected());
	//mNextImage->setEnabled(this->isSelected());
	update();
}
